package parser

import (
	"crawler/engine"
	"regexp"
	"strings"
)

const cityListRe = `{"linkContent":"([^"]+)","linkURL":"([^"]+)"}`

//const cityListRe = `<a href="(http://www.zhenai.com/zhenghun/[a-zA-Z]+)"[^>]+>([^<]+)</a>`

// 解析城市列表
func ParseCityList(contents []byte) engine.ParserResult {
	re := regexp.MustCompile(cityListRe)        //MustCompile：知道一定会匹配
	matches := re.FindAllSubmatch(contents, -1) //Submatch 拆分匹配
	result := engine.ParserResult{}
	limit := 2
	for _, match := range matches {
		result.Items = append(result.Items, "City "+string(match[1]))
		result.Requests = append(result.Requests, engine.Request{
			Url: strings.Replace(string(match[2]), `\u002F`, "/", -1),
			//Url:        string(match[1]),
			ParserFunc: ParseCity,
		})
		limit--
		if limit == 0 {
			break
		}
	}
	return result
}
